package com.oa.user.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.oa.organization.entity.Organization;
import com.oa.system.entity.Group;
import com.oa.system.entity.Role;
import com.oa.system.entity.UserInfo;
import com.oa.system.entity.UserInfo.Sex;

public class UserInfoDto {
	private Long id;
	private String fullname;
	private Sex sex;
	private int age;
	@JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")
	private Date birthday;
	private String email;
	private String phone;
	private String address;
	private String cover;
	private String dept;
	private Long deptId;
	private String job; // 职位
	@JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")
	private Date enterTime; // 入职日期
	private String role;
	private Long roleId;
	private List<Group> group = new ArrayList<>();

	@JsonIgnore
	private Organization organization;
	@JsonIgnore
	private Role roleOwn;

	public UserInfoDto() {
	}

	public Long getId() {
		return id;
	}

	public String getFullname() {
		return fullname;
	}

	public Sex getSex() {
		return sex;
	}

	public int getAge() {
		return age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getEmail() {
		return email;
	}

	public String getPhone() {
		return phone;
	}

	public String getAddress() {
		return address;
	}

	public String getCover() {
		return cover;
	}

	public String getDept() {
		return dept;
	}

	public Long getDeptId() {
		return deptId;
	}

	public String getJob() {
		return job;
	}

	public Date getEnterTime() {
		return enterTime;
	}

	public String getRole() {
		return role;
	}

	public Long getRoleId() {
		return roleId;
	}

	public List<Group> getGroup() {
		return group;
	}

	public Organization getOrganization() {
		return organization;
	}

	public Role getRoleOwn() {
		return roleOwn;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public void setSex(Sex sex) {
		this.sex = sex;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setCover(String cover) {
		this.cover = cover;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public void setEnterTime(Date enterTime) {
		this.enterTime = enterTime;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public void setGroup(List<Group> group) {
		this.group = group;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public void setRoleOwn(Role roleOwn) {
		this.roleOwn = roleOwn;
	}

	public static UserInfoDto toUserInfoDto(UserInfo userInfo) {
		UserInfoDto userInfoDto = new UserInfoDto();
		userInfoDto.setId(userInfo.getId());
		userInfoDto.setFullname(userInfo.getFullname());
		userInfoDto.setAddress(userInfo.getAddress());
		userInfoDto.setAge(userInfo.getAge());
		userInfoDto.setCover(userInfo.getCover());
		userInfoDto.setBirthday(userInfo.getBirthday());
		userInfoDto.setEmail(userInfo.getEmail());
		userInfoDto.setEnterTime(userInfo.getEnterTime());
		userInfoDto.setSex(userInfo.getSex());
		userInfoDto.setJob(userInfo.getJob());
		userInfoDto.setPhone(userInfo.getPhone());

		if (userInfo.getDept() != null) {
			userInfoDto.setDept(userInfo.getDept().getName());
			userInfoDto.setDeptId(userInfo.getDept().getId());
		}

		if (userInfo.getRole() != null) {
			userInfoDto.setRole(userInfo.getRole().getRole());
			userInfoDto.setRoleId(userInfo.getRole().getId());
			userInfoDto.setGroup(userInfo.getRole().getGroup());
		}

		return userInfoDto;
	}

	public static Specification<UserInfo> getUserManagerQuery(UserInfoDto userInfoDto) {
		return new Specification<UserInfo>() {
			@Override
			public Predicate toPredicate(Root<UserInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (userInfoDto.getFullname() != null && userInfoDto.getFullname().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("fullname").as(String.class),
							"%" + userInfoDto.getFullname() + "%");
					list.add(predicate);
				}
				if (userInfoDto.getDept() != null && userInfoDto.getDept().trim().length() > 0) {
					Predicate predicate = cb.equal(root.get("dept").as(Organization.class),
							userInfoDto.getOrganization());
					list.add(predicate);
				}
				if (userInfoDto.getJob() != null && userInfoDto.getJob().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("job").as(String.class), "%" + userInfoDto.getJob() + "%");
					list.add(predicate);
				}
				if (userInfoDto.getRole() != null && userInfoDto.getRole().trim().length() > 0) {
					Predicate predicate = cb.equal(root.get("role").as(Role.class), userInfoDto.getRoleOwn());
					list.add(predicate);
				}

				Predicate[] predicates = new Predicate[list.size()];
				return cb.and(list.toArray(predicates));
			}
		};
	}

	public static Specification<UserInfo> getRoleManagerQuery(UserInfoDto userInfoDto) {
		return new Specification<UserInfo>() {

			@Override
			public Predicate toPredicate(Root<UserInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (userInfoDto.getFullname() != null && userInfoDto.getFullname().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("fullname").as(String.class),
							"%" + userInfoDto.getFullname() + "%");
					list.add(predicate);
				}
				if (userInfoDto.getDept() != null && userInfoDto.getDept().trim().length() > 0) {
					Predicate predicate = cb.equal(root.get("dept").as(Organization.class),
							userInfoDto.getOrganization());
					list.add(predicate);
				}
				if (userInfoDto.getJob() != null && userInfoDto.getJob().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("job").as(String.class), "%" + userInfoDto.getJob() + "%");
					list.add(predicate);
				}

				Predicate[] predicates = new Predicate[list.size()];
				return cb.and(list.toArray(predicates));
			}
		};
	}

	@Override
	public String toString() {
		return "UserInfoDto [id=" + id + ", fullname=" + fullname + ", sex=" + sex + ", age=" + age + ", birthday="
				+ birthday + ", email=" + email + ", phone=" + phone + ", address=" + address + ", cover=" + cover
				+ ", dept=" + dept + ", deptId=" + deptId + ", job=" + job + ", enterTime=" + enterTime + ", role="
				+ role + "]";
	}

}
